//
//  APPLEEmoticonKeyboard.swift
//  123
//
//  Created by pangjiaxing on 16/1/13.
//  Copyright © 2016年 pangjiaxing. All rights reserved.
//

import UIKit
import SnapKit
private let APPLEEmoticonCollectionViewCellIdentifier:String = "APPLEEmoticonCollectionViewCell"
class APPLEEmoticonKeyboard: UIView {
    
    private lazy var  toolBar:APPLEEmoticonToolBar = {
       let toolBar = APPLEEmoticonToolBar(frame: CGRectZero)
        toolBar.delegate = self
        return toolBar
    }()
    
    private lazy var  emoticonCollectionView:APPLEEmoticonCollectionView = {
      let collectionView = APPLEEmoticonCollectionView()
      collectionView.dataSource = self
        collectionView.delegate = self
      collectionView.registerClass(APPLEEmoticonCollectionViewCell.self, forCellWithReuseIdentifier:APPLEEmoticonCollectionViewCellIdentifier)
      return collectionView
    }()
    
    private lazy var pageControl:UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.userInteractionEnabled = false
        // 选中的图片
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKeyPath: "_currentPageImage")
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKeyPath: "_pageImage")
        return pageControl
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        addSubview(toolBar)
        addSubview(emoticonCollectionView)
        addSubview(pageControl)
        toolBar.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(self)
            make.left.equalTo(self)
            make.bottom.equalTo(self)
        }
        
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar.snp_top)
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.trailing.equalTo(self)
        }
        pageControl.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar.snp_top).offset(-5)
            make.centerX.equalTo(self)
        }
      
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            let indexPath:NSIndexPath = NSIndexPath(forItem: 0, inSection: 1)
            self.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
        }
    }
}

extension APPLEEmoticonKeyboard : UICollectionViewDataSource{
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return APPLEEmoticonTools.sharedTools.allEmoticon.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return APPLEEmoticonTools.sharedTools.allEmoticon[section].count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(APPLEEmoticonCollectionViewCellIdentifier, forIndexPath: indexPath) as! APPLEEmoticonCollectionViewCell
        cell.indexPath = indexPath
        cell.emoticon = APPLEEmoticonTools.sharedTools.allEmoticon[indexPath.section][indexPath.item]
        return cell
    }
}

extension APPLEEmoticonKeyboard:UICollectionViewDelegate{
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let cells = emoticonCollectionView.visibleCells()
        if cells.count == 2 {
            let firstCell = cells.first
            let secondCell = cells.last
            let leftFirst = abs(Int32(firstCell!.frame.origin.x - scrollView.contentOffset.x))
            let leftSecond = abs(Int32(secondCell!.frame.origin.x - scrollView.contentOffset.x))
            
//            var section = 0
            var indexPath:NSIndexPath = NSIndexPath()

            if leftFirst < leftSecond {
//                section = (emoticonCollectionView.indexPathForCell(firstCell!)?.section)!
                indexPath = emoticonCollectionView.indexPathForCell(firstCell!)!
            }else{
//                section = (emoticonCollectionView.indexPathForCell(secondCell!)?.section)!
                indexPath = emoticonCollectionView.indexPathForCell(secondCell!)!
            }
            
//            print(section)
            
            toolBar.selectedButtonWithSection(indexPath.section)
            
            selectedPageController(indexPath)
        }
    }
    private func selectedPageController(indexpath:NSIndexPath){
        if (indexpath.section == 0){
            pageControl.hidden = true
        }else{
            pageControl.hidden = false
            pageControl.numberOfPages = APPLEEmoticonTools.sharedTools.allEmoticon[indexpath.section].count
            
            pageControl.currentPage = indexpath.item
        }
        
        
    }
}

extension APPLEEmoticonKeyboard:APPLEEmoticonToolBarDelegate{
    func emoticonButtonSelectedWithType(type: APPLEEmoticonType) {
        
        print(type)
        let indexPath:NSIndexPath = NSIndexPath(forItem: 0, inSection: type.rawValue-1000)
        emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
        
    }
}